% Creation of mean velocity running average plot with 95% confidence
% interval

% Definition of keywords
keywords = struct;
keywords.any_keywords = {};
keywords.with_keywords = {};
keywords.not_keywords = {};

% Define running average parameters
L_min = 1.0; %mum
L_max = 2.0; %mum
windows = 50; %number of running average windows
window_width = 0.15; % Fraction of the overall length range used as
%windows width

complex_in = true; % Include complex results, important for short
%filaments
non_dropped = false; % Apply selection mask from TraceDropper procedure

% Definition of filament level function. This function will be applied to
% every section, and the results should arrive in either a cell array or a
% numeric array form. In this example, we get from each section a numeric
% array that contains the scalar trace-based velocity of each filament.
filament_function = @(section) [section.trace_results.trace_velocity];

% Definition of window level function. This function is used to determine
% the results for every running average window. The results will be stored
% as elements of a cell array, where the cell array has one element for
% each running average window. The results therefore are not restricted to
% scalar values, but can be any valid object.
% In this case we make a histogram of all frame-to-frame velocities from
% all filaments that are contained in the length window

% Definitions to have the same histogram procedure in every length window
window_function = @(filament_results) mean(filament_results);

% Choose an input bundle from disk
[load_file,load_path] = ...
    uigetfile('*.mat','Select result bundle file from disk.');
result_bundle = [load_path load_file];

[analysis_output,window_centers,window_counts,CIntervals,bs_samples] = ...
    apply_length_resolved( ...
    result_bundle,keywords,L_min,L_max,windows,window_width,...
    complex_in,non_dropped, ...
    filament_function,window_function,'verbose');

% Cast the analysis output cell array into a numeric array
velocities = [analysis_output{:}];

% Plot the running average mean velocity
plot(window_centers,velocities,'k.-')
hold on

% Plot all bootstrap resamples of the length-resolved velocity mean
[number_of_samples,~] = size(bs_samples);
for bb = 1:number_of_samples
   plot(window_centers,bs_samples(bb,:),'r--') 
end
xlabel('L[mum]')
ylabel('Filament count')